# 指定CMake最低版本
cmake_minimum_required(VERSION 3.10)

# 设置项目名称
project(EquipServer)

# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 强制使用UTF-8编码（适用于CMake 3.12+）
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -finput-charset=UTF-8")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finput-charset=UTF-8")

# 或者使用add_compile_options（更现代的方式）
add_compile_options(-finput-charset=UTF-8)

# 如果需要Windows控制台支持UTF-8
if(WIN32)
    add_compile_definitions(_CRT_SECURE_NO_WARNINGS)  # 禁用不安全函数警告
    add_compile_options(-fexec-charset=UTF-8)  # Windows控制台默认使用GBK(CP936)
    # 或者使用以下代码在程序中设置控制台编码
    # add_executable(YourApp main.cpp)
    # target_link_libraries(YourApp PRIVATE ${CMAKE_DL_LIBS})
endif()


# 查找所有源文件
file(GLOB_RECURSE SOURCES "src/*.cpp")

# 添加可执行文件
add_executable(${PROJECT_NAME} ${SOURCES})

# 添加头文件搜索路径
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/spdlog
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/plog
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/rapidjson/include
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/modbus/include
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/mqtt/include
)

# 手动设定库的搜索路径
set(MODBUS_LIBRARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/modbus/lib)
set(PAHO_MQTT_LIBRARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/mqtt/lib/)

# 查找库文件
find_library(MODBUS_LIBRARY NAMES libmodbus.dll.a PATHS ${MODBUS_LIBRARY_DIR})
find_library(PAHO_MQTT_LIBRARY NAMES libpaho-mqtt3c.dll.a PATHS ${PAHO_MQTT_LIBRARY_DIR})

# 确保找到了库
if(NOT MODBUS_LIBRARY )
    message(FATAL_ERROR "无法找到Modbus的库")
endif()
if(NOT PAHO_MQTT_LIBRARY)
    message(FATAL_ERROR "无法找到PAHO_MQTT的库")
endif()

# 链接库文件
target_link_libraries(${PROJECT_NAME} PRIVATE
    ${MODBUS_LIBRARY}          # libmodbus库
    ${PAHO_MQTT_LIBRARY}       # MQTT库
    ws2_32          # Windows网络库
    # 其他库...
)

# 设置可执行文件输出目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)


# 配置Windows特定设置
if(WIN32)
    # 静态链接以避免DLL依赖
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")
endif()
